package com.star.vrserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.vrserver.api.R;
import com.star.vrserver.entity.Vaccarticle;
import com.star.vrserver.entity.query.VaccarticleQuery;
import com.star.vrserver.service.VaccarticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author star
 * @since 2023-03-09
 */
@Api(tags = "疫苗接种知识普及信息管理Api")
@RestController
@RequestMapping("/vaccarticle")
public class VaccarticleController {
    @Autowired
    private VaccarticleService vaccarticleService;

    //获取所有疫苗预约知识信息
    @ApiOperation(value = "获取所有疫苗预约知识信息")
    @GetMapping("getAllVaccarticles")
    public R getVaccarticles() {
        return R.ok().data("VaccarticleLists", vaccarticleService.list());
    }

    //根据id查询疫苗预约知识信息
    @ApiOperation(value = "根据id查询疫苗预约知识信息")
    @GetMapping("getVaccarticle")
    public R getVaccarticle(@RequestParam String id) {
        return R.ok().data("vaccarticleInfo", vaccarticleService.getById(id));
    }

    //根据id删除疫苗预约知识
    @ApiOperation(value = "根据id删除疫苗预约知识")
    @DeleteMapping("deleteVaccarticle/{id}")
    public R deleteVaccarticle(@PathVariable String id) {
        //是否删除成功：逻辑删除
        boolean flag = vaccarticleService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //添加疫苗预约知识
    @ApiOperation(value = "添加疫苗预约知识")
    @PostMapping("saveVaccarticle")
    public R saveVaccarticle(@RequestBody Vaccarticle vaccarticle) {
        boolean flag = vaccarticleService.save(vaccarticle);
        return flag ? R.ok() : R.error();
    }

    //分页查询疫苗预约知识  current 当前页  limit 每页记录数
    @ApiOperation(value = "分页展示疫苗预约知识列表")
    @GetMapping("pageVaccarticle/{current}/{limit}")
    public R pageListVaccarticle(@PathVariable long current,
                                 @PathVariable long limit) {
        //创建page对象
        Page<Vaccarticle> vaccarticlePage = new Page<>(current, limit);
        //调用方法:底层封装，把分页所有数据封装到pageVaccarticle对象里面
        vaccarticleService.page(vaccarticlePage);
        //获取总记录数
        long total = vaccarticlePage.getTotal();
        //获取最终分页结果集
        List<Vaccarticle> records = vaccarticlePage.getRecords();
        //将结果组装返回前端
        return R.ok().data("total", total).data("records", records);
    }

    //条件查询带分页的方法
    @ApiOperation(value = "分页展示疫苗预约知识列表(带条件)")
    @PostMapping("pageVaccarticleCondition/{current}/{limit}")
    public R pageListVaccarticle(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false) VaccarticleQuery vaccarticleQuery) {
        return R.ok();
    }
    //修改疫苗预约知识信息
    @ApiOperation(value = "修改疫苗预约知识信息")
    @PostMapping("updateVaccarticle")
    public R updateVaccarticle(@RequestBody Vaccarticle vaccarticle) {
        boolean flag = vaccarticleService.updateById(vaccarticle);
        return flag ? R.ok() : R.error();
    }

}
